package com.example.keepserver.controller;

import com.example.keepserver.dto.UserDTO;
import com.example.keepserver.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户相关API控制器
 */
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Slf4j
public class UserController {
    
    private final UserService userService;
    
    /**
     * 获取前100个用户列表
     * @return 用户列表(最多100个)
     */
    @GetMapping
    public ResponseEntity<List<UserDTO>> getAllUsers() {
        try {
            log.info("请求获取前10个用户列表");
            List<UserDTO> users = userService.getAllUsers()
                    .stream()
                    .limit(10)
                    .collect(Collectors.toList());
            log.info("成功获取用户列表，返回用户数量: {}", users.size());
            return ResponseEntity.ok(users);
        } catch (Exception e) {
            log.error("获取用户列表失败", e);
            throw e;
        }
    }
    
    /**
     * 根据ID获取用户
     * @param userId 用户ID
     * @return 用户信息
     */
    @GetMapping("/{userId}")
    public ResponseEntity<UserDTO> getUserById(@PathVariable String userId) {
        try {
            log.info("请求获取用户: {}", userId);
            UserDTO user = userService.getUserById(userId);
            if (user != null) {
                return ResponseEntity.ok(user);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            log.error("获取用户信息失败, userId: {}", userId, e);
            throw e;
        }
    }
} 